package com.sw.study.springwork.transaction;

import com.sw.study.springwork.annotation.Autowired;
import com.sw.study.springwork.annotation.Service;
import com.sw.study.springwork.db.ConnectionUtils;

import java.sql.SQLException;

/**
 * Created by sw on 2020/4/6.
 * 事务控制
 */
@Service
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }
    // 开启手动事务控制
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }


    // 提交事务
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
    }


    // 回滚事务
    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
    }
}